package com.briup.mt.web.controller;

import com.briup.mt.bean.BaseConfig;
import com.briup.mt.bean.BaseFile;
import com.briup.mt.service.IBaseFileService;
import com.briup.mt.utils.ExcelUtils;
import com.briup.mt.utils.Message;
import com.briup.mt.utils.MessageUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 姜鑫
 * @version 1.0
 * @date 2020/12/16 20:10
 */
@Validated
@RequestMapping("/base_file")
@RestController
public class BaseFileController {
    @Autowired
    private IBaseFileService baseFileService;
    @ApiOperation(value = "导入到Excel中", notes = "注意！测试的时候请将地址粘贴到浏览器地址栏测试", produces = "application/octet-stream")
    @GetMapping("download")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileName", value = "文件名", paramType = "query"),})
    public void download(HttpServletResponse response, String fileName) throws Exception {
        // 2. 将excel导出
        // 2.1 excel头部数据
        String[] headList = new String[] {"id", "文件名", "组名", "执行文件","文件大小" ,"文件类型"};
        // 2.2 属性
        String[] fieldList = new String[] {"id", "fileName", "groupName", "extName", "fileSize","fileType" };
        // 2.3 数据列，需要转化
        List<Map<String, Object>> dataList = new ArrayList<>();
        List<BaseFile> list= baseFileService.query(fileName);
        for (BaseFile file : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", file.getId());
            map.put("fileName", file.getFileName());
            map.put("groupName", file.getGroupName());
            map.put("extName", file.getExtName());
            map.put("fileSize", file.getFileSize());
            map.put("fileType", file.getFileType());
            dataList.add(map);
        }
        // 2.4 导出文件名
        String excelName = "file";
        // 3. 调用工具方法导出Excel
        ExcelUtils.createExcel(response, excelName, headList, fieldList, dataList);
    }
    @GetMapping("select")
    public Message query(String fileName){
        List<BaseFile> list=baseFileService.query(fileName);
        return MessageUtil.success(list);
    }

    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(BaseFile base){
        baseFileService.saveOrUpdate(base);
        return MessageUtil.success("操作成功");
    }

    @DeleteMapping("deleteById")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value="用户编号",required =true, paramType = "query"),})
    public Message deleteById(@NotNull String id){
        baseFileService.deleteById(id);
        return MessageUtil.success("删除成功");
    }
}




